<?php

namespace addons\rexam\controller;

class Group extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\rexam\Group;
        $this->assign('pid', 1);
    }

    public function index(){
        if ($this->request->isAjax()) {
            $param = input();
            $where = array();
            $where['pid'] = $param['pid'];

            $one = $this->model
                ->where(['id'=>$param['pid']])
                ->find();

            $list = $this->model
                ->where($where)
                ->select();
            $len = strlen(count($list));

            $pick = array();
            foreach ($list as $k=>$vo){
                $pick[] = [
                    'label'=>sprintf("%0".$len."u", $k+1).'.'.$vo['name'],
                    'value'=>$vo['id'],
                ];
            }

            return $this->ajax_success('OK',array("total" => count($pick), "rows" => $pick, "one"=>$one));
        }
        $group_model = new \app\admin\model\rexam\Group;
        if($this->rexam_user['rexam_group_id']){
            $rexam_group = $group_model->where(['id'=>$this->rexam_user['rexam_group_id']])->find();
            $this->assign('rexam_group',$rexam_group);
        }
        return $this->view->fetch();
    }
}